<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="coinfaces">

        <title>在Pelican里显示数学公式</title>

    <link rel="stylesheet" href="../../theme/css/style.css">
    <link rel="stylesheet" href="../../theme/css/pygments.css">



</head>

<body>
    <div class="content">
<h1>在Pelican里显示数学公式</h1>    <p>
        under
            <a href="../../tags/blog/">blog</a>
            <a href="../../tags/pelican/">pelican</a>
            <a href="../../tags/mathjax/">mathjax</a>
    </p>
    <p>
        in <a href="../../categories/tech/">tech</a>
    </p>
    <p>Published: 2016-09-19</p>


    <p><a class="reference external" href="https://github.com/getpelican/pelican-plugins/tree/master/render_math">render_math</a>插件在Pelican站点里用<a class="reference external" href="https://www.mathjax.org/">MathJax</a>显示数学公式，我现在看到的源码是这么处理reStructuredText文档的：</p>
<ul class="simple">
<li>合并全局设置<tt class="docutils literal">MATH_JAX</tt>和默认设置为<tt class="docutils literal">mathjax_settings</tt>对象</li>
<li>将<tt class="docutils literal">mathjax_settings</tt>的内容填入MathJax JavaScript加载模板，得到一段可嵌入HTML文档的JavaScript代码</li>
<li>设置公式部分免于拼写错误检查</li>
<li>设置<a class="reference external" href="http://docutils.sourceforge.net/docs/ref/rst/directives.html#math">docutils的math指令</a>输出格式为<tt class="docutils literal">MathJax</tt></li>
<li>为所有包含math指令的文章和页面生成的内容嵌入MathJax加载脚本</li>
<li>在安装了<a class="reference external" href="https://www.crummy.com/software/BeautifulSoup/">beautifulsoup4</a>的前提下，处理摘要<ul>
<li>将摘要中被截断的公式补充完整</li>
<li>为含有公式的摘要嵌入MathJax加载脚本，如果一个页面包含多个有公式的摘要，该加载脚本也会被嵌入多次，好在它里面有避免重复加载MathJax的逻辑，虽然难看，但不会引起大的问题</li>
</ul>
</li>
</ul>
<p>在<a class="reference external" href="https://github.com/PurePelicanTheme/pure/">pure主题</a>的index.html模板中，有一段用于为文章列表添加MathJax脚本的<a class="reference external" href="http://jinja.pocoo.org/">jinja</a>代码：</p>
<div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">MATH</span> <span class="k">is</span> <span class="nf">defined</span> <span class="k">and</span> <span class="k">not</span> <span class="nv">MATH</span><span class="o">[</span><span class="s1">&#39;auto_insert&#39;</span><span class="o">]</span> <span class="cp">%}</span>
     <span class="cp">{%</span> <span class="k">if</span> <span class="nv">page</span> <span class="k">and</span> <span class="nv">page.mathjax</span> <span class="k">or</span> <span class="nv">article</span> <span class="k">and</span> <span class="nv">article.mathjax</span> <span class="cp">%}</span>
         <span class="cp">{%</span> <span class="k">set</span> <span class="nv">mathjax</span> <span class="o">=</span> <span class="kp">True</span> <span class="cp">%}</span>
     <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nb">loop</span><span class="nv">.last</span> <span class="k">and</span> <span class="nv">mathjax</span> <span class="k">is</span> <span class="nf">defined</span> <span class="cp">%}</span>
     <span class="cp">{{</span> <span class="nv">MATHJAXSCRIPT</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
<p>虽然里面用到了<tt class="docutils literal"><span class="pre">MATH['auto_insert']</span></tt>设置，但细看至少和现在的render_math代码没什么关系，因为render_math用的是<tt class="docutils literal">MATH_JAX</tt>全局设置，并且没有使用或设置文章的<tt class="docutils literal">mathjax</tt>属性，至少reStructuredText用户可以不用理会，因为render_math里的<tt class="docutils literal">auto_insert</tt>只对Markdown格式的文档有影响，并且本文开头分析了render_math如何处理reStructuredText文档，它已经把这部分工作做了。</p>
<p>如果浏览器支持<a class="reference external" href="https://www.w3.org/Math/">MathML</a>，则可以不用加载MathJax，因为这个动作对网络环境有一定的要求，但使用MathJax至少有三点好处：</p>
<ul class="simple">
<li>不用将LaTex转化成MathML，并且<a class="reference external" href="http://docutils.sourceforge.net/docs/user/config.html#math-output">docutils也没有理想的将LaTex转化成MathML的方案</a></li>
<li>不需要额外地判断浏览器对MathML的支持程度（可能不是件容易的事情）</li>
<li>考虑最坏的情况，使用MathJax但脚本加载失败，LaTex代码还有一定的可读性，使用MathML但浏览器不支持，就完全看不懂了</li>
</ul>
<p>（完）</p>

    </div>
<nav class="nav sidebar">
    <div class="siteinfo">
        <div class="sitename"><a href="../..">coinfaces</a></div>
        <div class="tagline">Another Side of Life</div>
    </div>

        <a href="../../categories/">categories</a>
        <a href="../..">&larr; Home</a>
</nav><footer class="footer">
    <span>
        &copy; jpli &middot;
        under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC-BY-SA 4.0</a>
    </span>
    |
    <span>
        Powered by:
        <a href="http://blog.getpelican.com/">Pelican</a>
    </span>
</footer></body>
</html>